﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;
using HZ.Supervise.Model.View;

namespace HZ.Supervise.Web.Enterprise
{
    public partial class SellAmountDetail : BasePage
    { 
        IBLL_Company ic = HZ.Supervise.BLLFactory.IBLL_Company();
        IBLL_Area ia = BLLFactory.IBLL_Area();
        bool isclick;
        public SellAmountDetail()
        {

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager_CompanyPS_PageChanged(object sender, EventArgs e)
        {
            //AspNetPager_CompanyPS.CurrentPageIndex = 1; 
            BindData();

        }

        /// <summary>
        /// 绑定数据
        /// modify  by张雄  2011.5.4
        /// </summary>
        public void BindData()
        {
            SuperviseQuery vs = new SuperviseQuery();
            SuperviseQuery vs2 = new SuperviseQuery();
            vs2 = (SuperviseQuery)Session[Request.QueryString["sesid2"]];
            if (vs2 != null)
            {
                vs = (SuperviseQuery)Session[Request.QueryString["sesid"]];
                vs.SortFields = "ProcureCatalogID";
            }
            else
            {
                vs = Queryby();
            }
            //vs = (SuperviseQuery)Session[Request.QueryString["sesid"]];
            //vs.SortFields = " ProcureCatalogID";
            vs.Flag = "0";
            int t = 0;

            System.Data.DataTable dt = ic.GetTotalDetailBySell(vs, this.AspNetPager_CompanyPS.CurrentPageIndex, AspNetPager_CompanyPS.PageSize, out t);

            AspNetPager_CompanyPS.RecordCount = t;

            this.gvwPSLInfo.PageSize = AspNetPager_CompanyPS.PageSize;
            this.gvwPSLInfo.DataSource = dt;
            this.gvwPSLInfo.DataBind();

            vs.Flag = "1";
            dt = ic.GetTotalDetailBySell(vs, 1, 0, out t);

            foreach (System.Data.DataRow dr in dt.Rows)
            {
                lblMemo.Text = string.Format("销售单：{0}个；销售金额：{1}元；", dr["SellCount"].ToString(), dr["SellAmount"].ToString());
            }
            Session[Request.QueryString["sesid2"]] = null;

        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            isclick = true;
            AspNetPager_CompanyPS.CurrentPageIndex = 1;
        }
        /// <summary>
        /// 分页    县区
        /// by张雄
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void CompanyPSL_PS_PageChanged(object sender, EventArgs e)
        {
            BindData();

        }



        /// <summary>
        /// 导出
        /// modify  by  张雄    2011.5.5
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            SuperviseQuery vs = new SuperviseQuery();
            if (!isclick)
            {
                vs = (SuperviseQuery)Session[Request.QueryString["sesid"]];
                vs.SortFields = "ProcureCatalogID";
            }
            else
            {
                vs = Queryby();
            }
            vs.Flag = "0";
            int t = 0;
            System.Data.DataTable dt = ic.GetTotalDetailBySell(vs, 1, 0, out t);


            ExportFile.Instance.ExportData(dt, gvwPSLInfo, this.Page);
        }



        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private SuperviseQuery Queryby()
        {
            SuperviseQuery vs = new SuperviseQuery();

            vs = (SuperviseQuery)Session[Request.QueryString["sesid"]];
            vs.ProductName = "";
            switch (vs.BackPage.ToLower())
            {
                case "hospital":
                    vs.ProcureCatalogID = "";
                    break;
                case "comps":
                    vs.ProcureCatalogID = "";
                    break;
                case "comsc":
                    vs.ProcureCatalogID = "";
                    break;
                case "area":
                    vs.ProcureCatalogID = "";

                    divtip.Visible = true;
                    string areaid = vs.AreaID.Substring(vs.AreaID.IndexOf("'") + 1, vs.AreaID.LastIndexOf("'") - vs.AreaID.IndexOf("'") - 1);
                    Area area = ia.GetArea(areaid);
                    lblAreaInfo.Text = "当前查询地区：" + area.AreaName;
                    break;
                case "goods":
                    break;
            }
            vs.SortFields = " ProcureCatalogID";// OrderFileds.SelectedValue + " " + OrderType.SelectedValue;

            vs.HospitalName = txtHospitalName.Text.Trim();

            vs.CompanyName_PS = txtCompanyName_PS.Text.Trim();

            vs.CompanyName_SC = txtCompanyName_TB.Text.Trim();

            if (HZ.Common.zhHansUtil.HaveChineseCode(txtProductName.Text.Trim()))
            {
                vs.ProductName = txtProductName.Text.Trim();
            }
            else
            {
                vs.PinyinCode = txtProductName.Text.Trim();
            }
            if (string.IsNullOrEmpty(vs.ProcureCatalogID))
            {
                vs.ProcureCatalogID = txtProcureCatalogID.Text.Trim();
            }

            vs.MedicineModelName = txtMedicineModel.Text.Trim();
            vs.Pack = txtPack.Text.Trim();
            if (string.IsNullOrEmpty(vs.AreaID))
            { 
                vs.AreaID = HZ.Supervise.Web.Function.GetAreaIDBySQL(CurrentAccount.GetUser().Org.Area.AreaID);
            }
            
            return vs;
        }



        protected void btnBack_Click(object sender, EventArgs e)
        {
            string url = Request.QueryString["returnUrl"];

            Response.Redirect(url);
        }

    }
}
